/*
 * @Author: Superficial
 * @Date: 2021-10-22 15:38:45
 * @LastEditTime: 2022-06-01 00:37:11
 * @Description: 基本数据转换
 */
import { isNumberString, isNumber, isDateString } from 'class-validator'

export function unknownToNumber(value: unknown): number | unknown {
  return isNumberString(value) ? Number(value) : value
}

export function numberToBoolean(value: number): boolean | number {
  return isNumber(value, {
    allowNaN: false,
    allowInfinity: false,
  })
    ? Boolean(value)
    : value
}

// https://www.progress.com/blogs/understanding-iso-8601-date-and-time-format
export function unknowToDate(value: unknown): Date | unknown {
  return isDateString(value) ? new Date(value as string) : value
}
